<?php

namespace App\Admin\Renderable;

use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Models\Administrator;
use App\Admin\Actions\EcpmFilter;
use App\Admin\Controllers\Helpers\LinkHelper;
use App\Constant\AdrewardEnum;
use App\Models\AdrewardDetail;
use Dcat\Admin\Form;

use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class AdrewardDetailsTable extends LazyRenderable
{

    protected $userId;


    public function __construct($userId = 0)

    {

        $this->userId = $userId;

    }

    public function grid(): Grid
    {
        $this->userId = $this->user_id ?? null;



        return Grid::make(new AdrewardDetail(), function (Grid $grid) {

            // 动态过滤用户ID

            if ($this->userId) {

                $grid->model()->where('user_id', $this->userId);

            }

          //  $grid->quickSearch('id', 'user_id', 'user.uuid', 'user.nickname')->placeholder('输入 id\uuid\用户昵称\uid')->auto(false);;

            $grid->model()->with(['user']);
            $grid->selector(function (Grid\Tools\Selector $selector) {

                $selector->select('adtype_id', '广告类型', DB::table('adtypes')->pluck('name', 'id')->toArray());
                $selector->select('adplatform_name', '平台名称', DB::table('adreward_details')->where('adplatform_name', '<>', '')->groupBy('adplatform_name')->pluck('adplatform_name', 'adplatform_name')->toArray());

            });
            $grid->model()->orderBy('id', 'desc');

            $grid->column('adreward_id','收益记录ID')->sortable();
            $grid->column('user_id');
            $grid->column('appcode', 'APP版本')->label();
            $grid->column('amount')->sortable();
            $grid->column('ecpm', 'ECPM')->sortable();
            $grid->column('adplatform_name', '平台名称');
            $grid->column('adtype_name','广告类型');
         //   $grid->column('admodel_value');
            $grid->column('user_ip', '用户IP');
            $grid->column('collect_status', '明细状态')
                ->using(AdrewardEnum::STATUS_EXPLAINS)
                ->label(AdrewardEnum::STATUS_COLORS)
                ->sortable();
            $grid->column('displayed_at','广告展示时间')->sortable();
            $grid->column('created_at')->sortable();
            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableActions();
            $grid->fixColumns(1, 0);
        });
    }
}
